package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.StringUtil;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

/**
 * @ClassName: YunShangFlowTask
 * @Description: 云上流量商城查询接口
 * @author CXJ
 * @date 2017年4月8日 上午11:40:27
 * @version 1.0
 */
@Component
public class YunShangFlowTask
{
    /**
     * 日志
     */
    private static final Logger log = Logger.getLogger(YunShangFlowTask.class);
    
    /**
     * 充值成功
     */
    private static final String SUCCESS = "SUCCESS";
    
    /**
     * 充值失败
     */
    private static final String FAILED = "FAILED";
    
    /**
     * 订单充值中
     */
    private static final String UNDERWAY = "UNDERWAY";
    
    /**
     * 订单不存在
     */
    private static final String ORDERNOTEXIST = "ORDER_NOT_EXIST";
    
    @Autowired
    OrderDao orderDao;
    
    @Autowired
    RabbitMqProducer mqProducer;// 消息队列生产者
    
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;// 物理通道信息
    
//    @Scheduled(fixedDelay = 60000)
    public void queryYunShangOrder()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = 2;
        try
        {
            String providerId = "0000000071";//
            Map<String, Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);// 获取通道
            String defaultParameter = (String)channel.get("default_parameter");// 参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String linkUrl = paramMap.get("queryOrderUrl");// 查询地址
            String supplierId = paramMap.get("supplierId");
            // 密钥
            String secretKey = paramMap.get("privateKey");
            // 查询订单中属于指通的折扣
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("dispatcherProviderId", providerId);
            param.put("status", "1");
            List<Map<String, Object>> orderList = orderDao.selectByTask(param);// 查询出所有尚通的充值中的订单
            for (int i = 0, size = orderList.size(); i < size; i++)
            {
                Map<String, Object> order = orderList.get(i);
                // 渠道订单号
                String tbOrderNo = order.get("orderId") + "";
                map.put("orderId", tbOrderNo);
                String signStr = "supplierId" + supplierId + "tbOrderNo" + tbOrderNo + secretKey;
                // 签名
                String sign = MD5.ToMD5(signStr);
                String url = linkUrl + "?tbOrderNo=" + tbOrderNo + "&supplierId=" + supplierId + "&sign=" + sign;
                String result = ToolHttp.get(false, url);
                if (result != null)
                {
                    Map<String, String> resultMap = XmlUtils.readXmlToMap(result);
                    String coopOrderStatus = resultMap.get("coopOrderStatus");
                    String coopOrderNo = resultMap.get("coopOrderNo");
                    String errorCode = resultMap.get("errorCode");
                    String errorDesc = resultMap.get("errorDesc");
                    errorDesc = StringUtil.replaceBlank(errorDesc);
                    map.put("providerOrderId", coopOrderNo);
                    map.put("resultCode", errorCode + ":" + errorDesc);
                    if (SUCCESS.equals(coopOrderStatus))// 充值成功
                    {
                        flag = 1;
                    }
                    else if (FAILED.equals(coopOrderStatus))// 充值失败
                    {
                        flag = 0;
                    }
                    else if (UNDERWAY.equals(coopOrderStatus))// 提交成功>>>>充值中
                    {
                        continue;
                    }
                    else if (ORDERNOTEXIST.equals(coopOrderStatus))// 订单不存在
                    {
                        flag = -1;
                    }
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        }
        catch (Exception e)
        {
            log.error("云上流量商城查询出错" + e);
        }
    }
}
